package cn.factorybox.item.service.impl;

import cn.factorybox.common.utils.UUIDUtil;
import cn.factorybox.item.mapper.DeviceDataMapper;
import cn.factorybox.item.mapper.DeviceMapper;
import cn.factorybox.item.service.DeviceService;
import cn.factorybox.model.entity.DeviceData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.ObjectName;
import java.util.*;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-04 17:45
 */

@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public Map<String, Object> selectList() {
        List<Map<String,Object>> list = deviceMapper.selectDevice();
        Map<String,Object> maps = new HashMap<>();
        Map<String,Object> mapList = new HashMap<>();
        Map<String,Object> count = new HashMap<>();
        int all = list.size();
        int office = 0;
        int laboratory = 0;
        int outdoor = 0;
        for (Map<String,Object> map : list) {
            if ((Integer) map.get("type") == 0) {
                office ++;
            } else if ((Integer) map.get("type") == 1) {
                laboratory ++;
            } else if ((Integer) map.get("type") == 2) {
                outdoor ++;
            }
            //插入每个设备最新的十条温度数据
            List<Map<String, Object>> datas = deviceDataMapper.selectTenTempDatas((String) map.get("value"));
            List<Double> tempDatas = new ArrayList<>();
            for (Map<String,Object> map1 : datas) {
                tempDatas.add((Double)map1.get("temp"));
            }
            map.put("style",tempDatas);
        }
        count.put("all",all);
        count.put("office",office);
        count.put("laboratory",laboratory);
        count.put("outdoor",outdoor);
        mapList.put("devicelist",list);
        mapList.put("count",count);
        maps.put("data",mapList);
        return maps;
    }

    @Override
    public int insert(Map<String, Object> map) {
        map.put("id", UUIDUtil.uuid());
        map.put("pass",randomCode());
        map.put("time",new Date());
        return deviceMapper.insert(map);
    }

    @Override
    public int checkDevice(String fSerialNumber) {
        return deviceMapper.selectDeviceNum(fSerialNumber);
    }

    @Override
    public int update(Map<String, Object> map) {
        return deviceMapper.update(map);
    }

    @Override
    public Map<String, Object> getConnect(String fSerialNumber,String pass) {
        Map<String,Object> map = deviceMapper.getConnect(fSerialNumber,pass);
        return map;
    }

    @Override
    public int deleteDevice(String id) {
        return deviceMapper.deleteDevice(id);
    }

    @Override
    public Map<String, Object> selectList2() {
        List<Map<String,Object>> list = deviceMapper.selectDeviceList();
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return map;
    }

    @Override
    public Map<String, Object> selectCamera() {
        List<Map<String,Object>> list = deviceMapper.selectCamera();
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        return map;
    }


    //生成随机的六位识别码
    public static String randomCode() {

        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
       return str.toString();
    }
}
